#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from adds.comm.common import output_action_result
from adds.comm.const import ADDSCode
from adds.comm.log import log
from adds.service.adds_service_base import ADDSServiceBase
from adds.service.resource.parse_resource_param import ResourceParam
from adds.comm.utils import check_powershell_result
from common.common_models import JobPermissionInfo


class ADDSResourceService(ADDSServiceBase):
    def __init__(self, req_id: str, job_id: str, sub_id: str):
        super().__init__(req_id, job_id, sub_id)
        self.param = ResourceParam(self.req_id)

    def check_application(self):
        """
        功能描述：检查否开启了AD域服务
        """
        log.info(f"Enter check_application, req:{self.req_id}.")
        # 检查Windows服务器是否开启了AD域服务，未开启直接返回失败
        ps_command = 'Get-WindowsFeature AD-Domain-Services | Select-Object Name, InstallState'
        if check_powershell_result(ps_command, "Installed"):
            output_action_result(self.req_id, ADDSCode.SUCCESS.value, ADDSCode.SUCCESS.value, f"ADDS server is OK")
            return
        output_action_result(self.req_id, ADDSCode.RESOURCE_IS_NOT_EXIST.value,
                             ADDSCode.RESOURCE_IS_NOT_EXIST.value, f"ADDS server is abnormal")

    def query_job_permission(self):
        output = JobPermissionInfo(user="SYSTEM", group="adds", fileMode="0770")
        self.report_result(output.dict(by_alias=True))
